<?php

namespace app\admin\controller;
use app\admin\model\CancelReason as CancelReasonModel;
use think\Db;

class CancelReason extends Base {

    protected $cancel_reason_model;

    public function _initialize() {
        parent::_initialize();
        $this->cancel_reason_model = new CancelReasonModel();
    }

    /**
     * 评价配置列表
     * @param string
     * @param int
     * @return string
     */
    public function index() {
        $page = input('get.page');
        $limit = input('get.limit');
        if ($page && $limit) {
            $data = input('get.');
            $list = $this->cancel_reason_model->getListInfo($page, $limit, $data);
            echo json_encode($list);
            exit;
        }
        return $this->fetch();
    }

    
    //删除评价配置
    public function del() {
        if (input('get.id')) {
            $id = input('get.id');
            //更新删除活动信息
            $data = Db::name('cancel_reason')->where('id',$id)->update(['status'=>0]);
            if ($data) {
                exit(json_encode(['status' => 1, 'msg' => '删除成功']));
            } else {
                exit(json_encode(['status' => 0, 'msg' => '删除失败']));
            }
        } else {
            $this->error('不存在的评价信息', '', '', 1);
        }
    }

    

    //添加评价配置
    public function add() {
        if (input('post.')) {
            $data = input('post.');
            $data['admin_id'] = session('admin_id');
            $data['add_time'] = time();
            $coupon_id = Db::name('cancel_reason')->insertGetId($data);
            if ($coupon_id) {
                echo ajax_success('提交成功');
            } else {
                echo ajax_error('提交失败');
            }
        } else {
            return view();
        }
    }

    //修改评价配置
    public function edit() {
        if (input('post.')) {
            $data = input('post.');
            $data['update_admin_id'] = session('admin_id');
            $data['update_time'] = time();
            $id = Db::name('cancel_reason')->where('id', $data['id'])->update($data);
            if ($id) {
                echo ajax_success('编辑成功');
                die;
            } else {
                echo ajax_error('编辑失败');
                die;
            }
        } else {
            //查询评价信息
            $id = input('get.id');
            $info = Db::name('cancel_reason')->where('id', $id)->find();
            $this->assign('info', $info);
            return view();
        }
    }
}
